﻿/* ==============================================================================
 * 功能描述：PoolManager  
 * 
 * 创 建 者：郑镇泉
 * 
 * 创建日期：2016/4/4 19:27:53
 * ==============================================================================*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ZZQFrameWork;

public class PoolManager :Singleton<PoolManager>
{
    /// <summary>
    /// 子对象池集合
    /// </summary>
    private Dictionary<string, SubPool> subPoolDic = null;

    /// <summary>
    /// 所有子对象池的父物体
    /// </summary>
    private Transform parent;

    public Transform Parent
    {
        get
        {
            if (parent == null)
            {
                GameObject go = GameObject.Find("PoolManager");
                if (go == null)
                {
                    go = new GameObject("PoolManager");
                }
                parent = go.transform;
            }
            return parent;
        }
    }

    public override void Init()
    {
        subPoolDic=new Dictionary<string, SubPool>();
    }

    /// <summary>
    /// 根据key从对应对象池中取出物体
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public GameObject Spawn(string key)
    {
        if (!subPoolDic.ContainsKey(key))
        {
            CreatSubPool(key);
        }
        SubPool pool = subPoolDic[key];
        return pool.Spawn();
    }

    /// <summary>
    /// 把物体放入对应的对象池
    /// </summary>
    /// <param name="obj"></param>
    public void UnSpawn(GameObject obj)
    {
        foreach (KeyValuePair<string, SubPool> keyValuePair in subPoolDic)
        {
            if (keyValuePair.Value.HasObject(obj))
            {
                keyValuePair.Value.UnSpawn(obj);
            }
        }
    }

    /// <summary>
    /// 把所有属于对象池的物体都回收
    /// </summary>
    public void UnSpawnAll()
    {
        foreach (KeyValuePair<string, SubPool> keyValuePair in subPoolDic)
        {
            keyValuePair.Value.UnSpawnAll();
        }
    }

    /// <summary>
    /// 根据当前物品数量以及需要物品数量获取对应数量的物品
    /// </summary>
    /// <param name="key"></param>
    /// <param name="needCount"></param>
    /// <param name="nowList"></param>
    /// <returns></returns>
    public List<GameObject> SpawnNeedGameObjects(string key, int needCount, List<GameObject> nowList)
    {
        int nowCount = nowList.Count;
        if (nowCount > needCount)
        {
            for (int i = 0; i < nowCount-needCount; i++)
            {
                UnSpawn(nowList[i]);
                nowList.RemoveAt(i);
            }
        }
        else
        {
            for (int i = 0; i < needCount-nowCount; i++)
            {
                GameObject go = Spawn(key);
                nowList.Add(go);
            }
        }
        return nowList;
    }

    public List<T> SpawnNeedList<T>(string key, int needCount, List<T> nowList)where  T:Component 
    {
        int nowCount = nowList.Count;
        if (nowCount > needCount)
        {
            for (int i = 0; i < nowCount - needCount; i++)
            {
                UnSpawn(nowList[i].gameObject);
                nowList.RemoveAt(i);
            }
        }
        else
        {
            for (int i = 0; i < needCount - nowCount; i++)
            {
                GameObject go = Spawn(key);
                nowList.Add(go.GetComponent<T>());
            }
        }
        return nowList;
    }

    /// <summary>
    /// 创建新的子对象池
    /// </summary>
    /// <param name="key"></param>
    private void CreatSubPool(string key)
    {
        GameObject prefab = ResManager.Instance.Load(key)as GameObject;
        GameObject pa=new GameObject(key+"SubPool");
        pa.transform.parent = Parent;
        SubPool pool=new SubPool(prefab,pa.transform);
        subPoolDic.Add(key,pool);
    }

}